package com.ruitu.health.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.makeramen.roundedimageview.RoundedImageView;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ScreenUtils;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health.R;
import com.ruitu.health.mvp.model.HomeModel;
import com.ruitu.health.mvp.presenter.HomePresenter;
import com.ruitu.health_1.ui.ArticleDetailsActivity;
import com.ruitu.health_2.ui.activity.ChinaCulture2Activity;
import com.ruitu.health_2.ui.activity.CouponListActivity;
import com.ruitu.health_2.ui.activity.CultureTravel2Activity;
import com.ruitu.health_2.ui.activity.EduCulture2Activity;
import com.ruitu.health_2.ui.activity.HealthManage2Activity;
import com.ruitu.health_2.ui.activity.LifeColorActivity;
import com.ruitu.health_2.ui.activity.MsgActivity;
import com.ruitu.health_2.ui.activity.ProductDetailActivity;
import com.ruitu.health_2.ui.activity.ReadPartie2Activity;
import com.ruitu.health_2.ui.activity.ReadPartieDetailActivity;
import com.ruitu.health_2.ui.activity.SearchActivity;
import com.ruitu.health_2.ui.common.VideoDetailActivity;
import com.ruitu.health_2.ui.common.util.ViewUtil;
import com.ruitu.health_2.ui.mall.MallMainActivity;
import com.ruitu.router_module.Logg;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.home.Advert;
import com.ruitu.router_module.bean.home.HomeData;
import com.ruitu.router_module.bean.home.Model;
import com.ruitu.router_module.bean.home.ReadPartie;
import com.ruitu.router_module.bean.product.ArtiVideo;
import com.ruitu.router_module.bean.product.ProHome;
import com.ruitu.router_module.support.util.GlideImageLoader;
import com.ruitu.router_module.support.view.BaseRecyclerAdapter;
import com.ruitu.router_module.support.view.EndlessRecyclerOnScrollListener;
import com.ruitu.router_module.support.view.OnTabSelectedListenerImpl;
import com.ruitu.router_module.util.GlideUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import me.dkzwm.widget.srl.SmoothRefreshLayout;
import me.dkzwm.widget.srl.config.Constants;
import me.dkzwm.widget.srl.indicator.IIndicator;

// 首页第三版
public class Home3Fragment extends BaseFragment<HomePresenter.HomeP, HomeModel.HomeM> {
    private SmoothRefreshLayout refresh;// 外层刷新控件
    private RecyclerView rcv_mall_pro;// 商品列表 带header

    private FrameLayout home_title_bar_layout;// 主页顶部title
    private LinearLayout scan_layout, ll_search;// 标题栏左上角的,搜索
    private RelativeLayout msg_layout;// 右上角消息

    private Banner banner;
    private ImageView iv_banner_holder;// banner的占位图
    private RecyclerView rcv_10_menu, rcv_model;// 10个菜单项,八大模块

    private ImageView iv_new_1, iv_new_2;// 新人专享左,新人专享右

    private XTabLayout tab, tab_2;// tab tab_2是默认隐藏的tab
    private RelativeLayout rl_tab;// 包含tab_2和下边的分割线的布局
    /************************** 上面是view 下面是数据 **************************/
    private HomeData homeData;// 请求的商城首页数据

    private List<Advert> advertiseList = new ArrayList<>();// banner广告轮播数据

    private MenuAdapter menuAdapter;// 10个菜单项的adapter
    private List<Model> modelList = new ArrayList<>();// 栏目的数据

    private String currTabId = "1";// 记录当前选择的tabId 默认为1
    private int currPage = 1;// 当前tab下的商品页码
    private int totalPage = 1;// 当前tab下的商品总页数

    // tab对应三个商品列表和三个adapter
    private ArrayList<ProHome> mallProList = new ArrayList<>();// 商品列表
    private MallProListAdapter proAdapter;// 商品adapter
    private ArrayList<ReadPartie> readPartieList = new ArrayList<>();// 读书会列表
    //    private ModelReadPartieListAdapter readPartieAdapter;// 读书会adapter
    private ArrayList<ArtiVideo> artiVideoList = new ArrayList<>();// 文章视频混排列表
//    private ModelArticleListAdapter articleAdapter;// 文章adapter

    public static final String[] HOME_MODELS = {"精选", "读书会", "文章", "积分", "商品"};// 新五大模块的布局 精选 读书会...
    private int SCREEN_WIDTH = 0;// 屏幕的宽度
    private int tabHei = 0;// tab的高度 50dp
    private boolean isReqComplete = false;

    public static Home3Fragment newInstance() {
        Bundle args = new Bundle();
        Home3Fragment fragment = new Home3Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SCREEN_WIDTH = ScreenUtils.getScreenWidth();// 获取屏幕宽度
        tabHei = SizeUtils.dp2px(50);
    }

    View header;
    GridLayoutManager proLayoutManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home_3, container, false);
        refresh = v.findViewById(R.id.refresh);
        rcv_mall_pro = v.findViewById(R.id.rcv_mall_pro);
        home_title_bar_layout = v.findViewById(R.id.home_title_bar_layout);
        scan_layout = v.findViewById(R.id.scan_layout);
        ll_search = v.findViewById(R.id.ll_search);
        msg_layout = v.findViewById(R.id.msg_layout);
        rl_tab = v.findViewById(R.id.rl_tab);
        tab_2 = v.findViewById(R.id.tab_2);

        initRefreshView();

        header = inflater.inflate(R.layout.header_home_pro_layout, null, false);
        // Banner布局
        banner = header.findViewById(R.id.banner);// Banner布局
        iv_banner_holder = header.findViewById(R.id.iv_banner_holder);// Banner布局占位图
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) banner.getLayoutParams();
        params.width = SCREEN_WIDTH;
        params.height = params.width * 900 / 1800;// banner的比例是1800 * 900
        banner.setLayoutParams(params);
        iv_banner_holder.setLayoutParams(params);
        //菜单项的布局
        rcv_10_menu = header.findViewById(R.id.rcv_10_menu);
        menuAdapter = new MenuAdapter(getActivity());
        GridLayoutManager manager5 = new GridLayoutManager(getActivity(), 4);
        rcv_10_menu.setLayoutManager(manager5);
        rcv_10_menu.setAdapter(menuAdapter);
        for (int i = 0; i < 8; i++) {
            Model model = new Model();
            model.setTitle("加载中");
            modelList.add(model);
        }
        menuAdapter.setDataList(modelList);
        // 团购的布局

        // 新人专享模块 3张图片
        iv_new_1 = header.findViewById(R.id.iv_new_1);
        iv_new_2 = header.findViewById(R.id.iv_new_2);

        int[] size = ViewUtil.getHomeNewSpecial2Height(SCREEN_WIDTH);
        LinearLayout.LayoutParams paramIvNew = (LinearLayout.LayoutParams) iv_new_1.getLayoutParams();
        paramIvNew.width = size[0];
        paramIvNew.height = size[1];
        iv_new_1.setLayoutParams(paramIvNew);

        LinearLayout.LayoutParams paramIvNew2 = (LinearLayout.LayoutParams) iv_new_2.getLayoutParams();
        paramIvNew2.width = size[0];
        paramIvNew2.height = size[1];
        iv_new_2.setLayoutParams(paramIvNew2);

        // tab五个模块
        tab = header.findViewById(R.id.tab);
        // 默认设置五个tab
        for (int i = 0; i < 5; i++) {
            tab.addTab(tab.newTab().setText(HOME_MODELS[i]), i);
            tab_2.addTab(tab_2.newTab().setText(HOME_MODELS[i]), i);
        }

        proAdapter = new MallProListAdapter();
        proAdapter.setHeaderView(header);
        proLayoutManager = new GridLayoutManager(getContext(), 2);
        rcv_mall_pro.setLayoutManager(proLayoutManager);
        rcv_mall_pro.setAdapter(proAdapter);

        setListeners();
        reqData();
        showProgress();

        return v;
    }

    private void initRefreshView() {
        refresh.setEnableAutoLoadMore(true);
        refresh.setDurationToCloseHeader(500);
        refresh.setDurationOfBackToKeepHeader(300);
        refresh.setEnableOverScroll(false);
//        refresh.setLoadingMinTime(1000);// 最少刷新一秒
        refresh.setMode(Constants.MODE_DEFAULT);// MODE_SCALE 仿小米下拉拉伸页面的效果
    }

    private void reqData() {
        p.reqHomeData();
    }

    // 根据tab中的posId请求商品列表
    private void reqMallProList() {
        switch (currTabId) {
            case "1":
            case "4":
            case "5":
                p.reqHomeTab1(currTabId, 10, currPage);
                break;
            case "2":
                p.reqHomeTab2(currTabId, 10, currPage);
                break;
            case "3":
                p.reqHomeTab3(currTabId, 10, currPage);
                break;
        }
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {// 商城首页数据
            rcv_mall_pro.post(new TopHeightRunnable());// 计算顶部高度
            refresh.refreshComplete(true, 800);
            homeData = (HomeData) data;
            advertiseList = homeData.getAdvertList();

            setDataToView();

//            currTabId = currTabId;
            reqMallProList();
        }
        if (code == 11) {// 首页的商品列表
            PageModel2<ProHome> proPage = (PageModel2<ProHome>) data;
            totalPage = proPage.getTotalPage();
            List<ProHome> newData = proPage.getList();
            if (currPage == 1) {
                mallProList.clear();
            }
            if (!isDataEmpty(newData)) {
                mallProList.addAll(newData);
                isReqComplete = true;
            } else {
                isReqComplete = false;
            }
            proAdapter.setDatas(mallProList);
        }
        if (code == 12) {// 首页的读书会列表
            PageModel2<ReadPartie> readPage = (PageModel2<ReadPartie>) data;
            totalPage = readPage.getTotalPage();
            List<ReadPartie> newData = readPage.getList();
            if (currPage == 1) {
                readPartieList.clear();
            }
            if (!isDataEmpty(newData)) {
                readPartieList.addAll(newData);
            }
            proAdapter.setDatas(readPartieList);
        }
        if (code == 13) {// 首页的文章视频混排列表
            PageModel2<ArtiVideo> articlePage = (PageModel2<ArtiVideo>) data;
            totalPage = articlePage.getTotalPage();
            List<ArtiVideo> newData = articlePage.getList();
            if (currPage == 1) {
                artiVideoList.clear();
            }
            if (!isDataEmpty(newData)) {
                artiVideoList.addAll(newData);
            }
            proAdapter.setDatas(artiVideoList);
        }
    }

    private void setDataToView() {
        initBanner(homeData.getAdvertList());
        modelList = homeData.getModelList();
        menuAdapter.setDataList(modelList);// 设置栏目模块
        try {
            GlideUtil.loadImg(homeData.getActivityAndCoupon().get(0).getImgUrl(), iv_new_1);
            GlideUtil.loadImg(homeData.getActivityAndCoupon().get(1).getImgUrl(), iv_new_2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initBanner(final List<Advert> bannerList) {
        List<String> urlList = new ArrayList<>();//图片地址集合
        List<String> introList = new ArrayList<>();//简介集合
        if (!isDataEmpty(bannerList)) {
            for (Advert banner : bannerList) {
                urlList.add(banner.getImgUrl());
                introList.add("");//不显示banner简介
            }
        }
        if (isDataEmpty(urlList)) {
            banner.setVisibility(View.GONE);
            iv_banner_holder.setVisibility(View.VISIBLE);
            return;
        } else {
            banner.setVisibility(View.VISIBLE);
            iv_banner_holder.setVisibility(View.GONE);
        }
        try {
            banner.setImages(urlList);//设置图片集合
            banner.setImageLoader(new GlideImageLoader());//设置图片加载器
            banner.setDelayTime(6000);//设置轮播时间
            banner.setIndicatorGravity(BannerConfig.RIGHT);
            banner.setBannerAnimation(Transformer.Accordion);
            banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int pos) {
                    ToastUtils.showShortSafe("点击广告banner" + pos);
                }
            });
            banner.start();//banner设置方法全部调用完毕时最后调用
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        if (view == msg_layout) {
            startActivity(MsgActivity.class);
        }
        if (view == scan_layout) {

        }
        if (view == ll_search) {// 搜索
            startActivity(SearchActivity.class);
        }
        if (view == iv_new_1) {

        }
        if (view == iv_new_2) {// 优惠券
            startActivity(CouponListActivity.class);
        }
    }

    private void loadMorePro() {
        currPage++;
        if (currPage <= totalPage) {
            hideProgress();
            showProgressWithText(true, "更多数据加载中..");
            reqMallProList();
        } else {
//            ToastUtils.showShort("数据加载完成");
        }
    }

    int scrollY;// 记录页面实时滑动的y

    private void setListeners() {
        setOnClickListener(scan_layout, ll_search, msg_layout, iv_new_1, iv_new_2);
        menuAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int pos) {
                if (!isDataEmpty(modelList)) {
                    String id = modelList.get(pos).getId();
                    if (TextUtils.equals("2", id)) {// 商城
                        startActivity(MallMainActivity.class);
                    } else if (TextUtils.equals("3", id)) {// 读书会
                        startActivity(ReadPartie2Activity.class);
                    } else if (TextUtils.equals("4", id)) {// 中华文化
                        startActivity(ChinaCulture2Activity.class);
                    } else if (TextUtils.equals("5", id)) {// 品味生活
                        startActivity(LifeColorActivity.class);
                    } else if (TextUtils.equals("6", id)) {// 健康管理
                        startActivity(HealthManage2Activity.class);
                    } else if (TextUtils.equals("7", id)) {// 文化旅行
                        startActivity(CultureTravel2Activity.class);
                    } else if (TextUtils.equals("8", id)) {// 教育文化
                        startActivity(EduCulture2Activity.class);
                    } else if (TextUtils.equals("9", id)) {// 投资项目
                        ToastUtils.showShortSafe("敬请期待");
                    }
                } else {
                    ToastUtils.showShortSafe("请稍后重试");
                }
            }
        });
        rcv_mall_pro.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
                    manager = (GridLayoutManager) recyclerView.getLayoutManager();
                }
                int firstPosition = manager.findFirstVisibleItemPosition();
                scrollY = rcv_mall_pro.computeVerticalScrollOffset();
                if ((scrollY + tabHei > topHeight || firstPosition > 0) && isReqComplete) {// 最终方案
                    rl_tab.setVisibility(View.VISIBLE);
                } else {
                    rl_tab.setVisibility(View.GONE);
                }
            }

            @Override
            public void onLoadMore() {
                loadMorePro();
            }
        });
        refresh.setOnRefreshListener(new SmoothRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefreshing() {
                currPage = 1;
                totalPage = 1;
                reqData();
//                reqProListByPos();
                hideProgress();
                showProgress();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refresh.refreshComplete(false, 800);
                    }
                }, 15000);
            }

            @Override
            public void onLoadingMore() {
                Logg.i("refresh加载更多...");
            }
        });
        refresh.addOnStatusChangedListener(new SmoothRefreshLayout.OnStatusChangedListener() {
            @Override
            public void onStatusChanged(byte old, byte now) {
//                Logg.i("onStatusChanged... old = " + old + ",now = " + now);
                if (now == SmoothRefreshLayout.SR_STATUS_INIT) {// 正常状态,显示搜索栏
                    showTitleBar(true);
                } else if (now == SmoothRefreshLayout.SR_STATUS_PREPARE) {// 正在下拉
                    showTitleBar(false);
                } else if (now == SmoothRefreshLayout.SR_STATUS_REFRESHING) {// 正在刷新
                    showTitleBar(false);
                } else if (now == SmoothRefreshLayout.SR_STATUS_COMPLETE) {// 刷新完成(还没有恢复)
                    showTitleBar(false);
                } else if (now == SmoothRefreshLayout.SR_STATUS_LOADING_MORE) {// 看意思是加载更多
                    showTitleBar(true);
                }
            }
        });
        refresh.addOnNestedScrollChangedListener(new SmoothRefreshLayout.OnNestedScrollChangedListener() {
            @Override
            public void onNestedScrollChanged() {
//                Logg.i("onNestedScrollChanged...");
            }
        });
        refresh.addOnUIPositionChangedListener(new SmoothRefreshLayout.OnUIPositionChangedListener() {
            @Override
            public void onChanged(byte status, IIndicator indicator) {
                // 这个回调和addOnStatusChangedListener回调的状态值是一样的
                // 区别:OnStatusChanged状态相同时不执行,只有状态改变时执行 onChanged:一直回调当前状态
            }
        });
        tab.addOnTabSelectedListener(new OnTabSelectedListenerImpl() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                // 切换tab 处理原数据
//                if (TextUtils.equals("2", currTabId)) {
//                    readPartieList.clear();
//                    proAdapter.setDatas(readPartieList);
//                } else if (TextUtils.equals("3", currTabId)) {
//                    articleList.clear();
//                    proAdapter.setDatas(articleList);
//                } else {
//                    mallProList.clear();
//                    proAdapter.setDatas(mallProList);
//                }
                // 切换tab 请求新数据
                currTabId = tab.getPosition() + 1 + "";// 当前切换的tab对应的posId
                currPage = 1;
                totalPage = 1;
                if (TextUtils.equals("2", currTabId)) {
                    proAdapter.PRO_TYPE = 1;
                    rcv_mall_pro.setLayoutManager(new LinearLayoutManager(getActivity()));
                } else if (TextUtils.equals("3", currTabId)) {
                    proAdapter.PRO_TYPE = 2;
                    rcv_mall_pro.setLayoutManager(new LinearLayoutManager(getActivity()));
                } else {
                    proAdapter.PRO_TYPE = 0;
                    rcv_mall_pro.setLayoutManager(proLayoutManager);
                }

                reqMallProList();
                hideProgress();
                showProgress();
                // tab1 和 tab2 联动
                tab_2.getTabAt(tab.getPosition()).select();
            }
        });
        tab_2.addOnTabSelectedListener(new OnTabSelectedListenerImpl() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                // tab1 和 tab2 联动
                Home3Fragment.this.tab.getTabAt(tab.getPosition()).select();
            }
        });
        proAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object data) {
                try {
                    Bundle bundle = new Bundle();
                    if (TextUtils.equals("2", currTabId)) {
                        bundle.putString("name", readPartieList.get(position).getName());
                        bundle.putString("id", readPartieList.get(position).getId());
                        startActivity(ReadPartieDetailActivity.class, bundle);
                    } else if (TextUtils.equals("3", currTabId)) {
                        if (TextUtils.equals(artiVideoList.get(position).getType(), "1")) {// 文章
                            bundle.putString("id", artiVideoList.get(position).getId());
                            startActivity(ArticleDetailsActivity.class, bundle);
                        } else {// 视频
                            bundle.putString("videoId", artiVideoList.get(position).getId());
                            startActivity(VideoDetailActivity.class, bundle);
                        }
                    } else {
                        bundle.putString("id", mallProList.get(position).getId());
                        startActivity(ProductDetailActivity.class, bundle);
                    }
                } catch (Exception e) {
                    ToastUtils.showShortSafe("数据异常,请稍候重试");
                    e.printStackTrace();
                }
            }
        });
    }

    private void showTitleBar(boolean isShow) {
        if (isShow) {
            if (home_title_bar_layout.getVisibility() == View.GONE) {
                home_title_bar_layout.setVisibility(View.VISIBLE);
            }
        } else {
            if (home_title_bar_layout.getVisibility() == View.VISIBLE) {
                home_title_bar_layout.setVisibility(View.GONE);
            }
        }
    }

    //banner下面的菜单项的adapter
    private static class MenuAdapter extends ListBaseAdapter<Model> {

        private int[] menuIcon = {R.drawable.home_ic_menu_4_dfts, R.drawable.home_ic_menu_0_tyt
                , R.drawable.home_ic_menu_1_gyg, R.drawable.home_ic_menu_2_chj, R.drawable.home_ic_menu_3_cjqc
                , R.drawable.home_ic_menu_5_hyjp, R.drawable.home_ic_menu_6_tjsp, R.drawable.home_ic_menu_7_jjsj
                , R.drawable.home_ic_menu_8_yhq, R.drawable.home_ic_menu_9_grzx};

        public MenuAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.home_item_menu;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int pos) {
            TextView tv_menu_txt = holder.getView(R.id.tv_menu_txt);
            ImageView iv_menu_icon = holder.getView(R.id.iv_menu_icon);

            Model model = mDataList.get(pos);
            tv_menu_txt.setText(model.getTitle());
            iv_menu_icon.setImageResource(menuIcon[pos]);
        }
    }

    private class MallProListAdapter extends BaseRecyclerAdapter {
        public int PRO_TYPE = 0;// 0 商品 1 读书会 2 文章/视频混排

        @Override
        public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
            View v = null;
            v = View.inflate(getActivity(), R.layout.item_home_multi_pro, null);
//            return new MyHolder(v);
            return new SuperViewHolder(v);
        }

        @Override
        public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, Object data) {
            SuperViewHolder holder = (SuperViewHolder) viewHolder;

            RelativeLayout rl_pro = holder.getView(R.id.rl_pro);
            RelativeLayout rl_read = holder.getView(R.id.rl_read);
            RelativeLayout rl_article = holder.getView(R.id.rl_article);

            if (PRO_TYPE == 0) {// 商品
                rl_pro.setVisibility(View.VISIBLE);
                rl_read.setVisibility(View.GONE);
                rl_article.setVisibility(View.GONE);
                if (data instanceof ProHome) {
                    setProInfo((ProHome) data, holder);
                }

                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rl_pro.getLayoutParams();
                if (RealPosition % 2 == 0) {
                    params.leftMargin = SizeUtils.dp2px(10);
                    params.rightMargin = SizeUtils.dp2px(5);
                } else {
                    params.leftMargin = SizeUtils.dp2px(5);
                    params.rightMargin = SizeUtils.dp2px(10);
                }
                int num = mallProList.size();
                if (num % 2 == 0) {
                    if (RealPosition == 0 || RealPosition == 1) {
                        params.topMargin = SizeUtils.dp2px(10);
                        params.bottomMargin = SizeUtils.dp2px(5);
                    } else if (RealPosition == num - 1 || RealPosition == num - 2) {
                        params.topMargin = SizeUtils.dp2px(5);
                        params.bottomMargin = SizeUtils.dp2px(10);
                    } else {
                        params.topMargin = SizeUtils.dp2px(5);
                        params.bottomMargin = SizeUtils.dp2px(5);
                    }
                } else {
                    if (RealPosition == 0 || RealPosition == 1) {
                        params.topMargin = SizeUtils.dp2px(10);
                        params.bottomMargin = SizeUtils.dp2px(5);
                    } else if (RealPosition == num - 1) {
                        params.topMargin = SizeUtils.dp2px(5);
                        params.bottomMargin = SizeUtils.dp2px(10);
                    } else {
                        params.topMargin = SizeUtils.dp2px(5);
                        params.bottomMargin = SizeUtils.dp2px(5);
                    }
                }

            } else if (PRO_TYPE == 1) {// 读书会
                rl_pro.setVisibility(View.GONE);
                rl_read.setVisibility(View.VISIBLE);
                rl_article.setVisibility(View.GONE);
                if (data instanceof ReadPartie) {
                    setReadPartieInfo((ReadPartie) data, holder);
                }
            } else {// 文章/视频混排
                rl_pro.setVisibility(View.GONE);
                rl_read.setVisibility(View.GONE);
                rl_article.setVisibility(View.VISIBLE);
                if (data instanceof ArtiVideo) {
                    initArticle((ArtiVideo) data, holder);
                }
            }
        }

        private void initArticle(ArtiVideo data, SuperViewHolder holder) {
            ArtiVideo artiVideo = data;
            RoundedImageView icon = holder.getView(R.id.icon_article);
            TextView tv_title = holder.getView(R.id.tv_title_article);
            TextView tv_name = holder.getView(R.id.tv_name_article);
            TextView tv_time = holder.getView(R.id.tv_time_article);
            TextView tv_subtitle = holder.getView(R.id.tv_subtitle_article);

            tv_title.setText(artiVideo.getTitle());
            tv_name.setText(artiVideo.getArtName());
//            tv_time.setText(article.getUpdateTime());
            tv_subtitle.setText(artiVideo.getReserveOne());
            GlideUtil.loadImg(artiVideo.getImg(), icon);
        }

        private void setReadPartieInfo(ReadPartie data, SuperViewHolder holder) {
            ReadPartie read = data;
            RoundedImageView icon = holder.getView(R.id.icon_read);
            TextView tv_title = holder.getView(R.id.tv_title_read);
            TextView tv_name = holder.getView(R.id.tv_name_read);
            TextView tv_time = holder.getView(R.id.tv_time_read);
            TextView tv_subtitle = holder.getView(R.id.tv_subtitle_read);

            tv_title.setText(read.getName());
            tv_name.setText(read.getBrokerageName());
            tv_time.setText(read.getAddress());
            tv_subtitle.setText(read.getContent());
            GlideUtil.loadImg(read.getLogo(), icon);
        }

        private void setProInfo(ProHome data, SuperViewHolder holder) {
            ImageView icon = holder.getView(R.id.icon_pro);// 图片
            TextView tv_name = holder.getView(R.id.tv_name_pro);// 名称
            TextView tv_price = holder.getView(R.id.tv_price_pro);// 价格
            TextView tv_integral = holder.getView(R.id.tv_integral_pro);// 积分

            int width = ScreenUtils.getScreenWidth();
            int itemWidth = (width - SizeUtils.dp2px(30)) / 2;

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) icon.getLayoutParams();
            params.width = itemWidth;
            params.height = itemWidth;
            icon.setLayoutParams(params);

            ProHome pro = data;
            tv_name.setText(pro.getName());
            tv_price.setText("￥" + pro.getPrice());
            GlideUtil.loadImg(pro.getPic(), icon);

            try {
                if (Integer.parseInt(pro.getGiftPoint()) > 0) {
                    tv_integral.setText("送" + pro.getGiftPoint() + "积分");
                } else {
                    tv_integral.setText("");
                }
            } catch (Exception e) {
                e.printStackTrace();
                tv_integral.setText("");
            }
        }
    }

    private int tabY = 0;// tab的y坐标
    private int topHeight = 0;// 记录页面上部的高度(banner + menu + 3图 + tab + 上下外边距)

    // 计算首页顶部高度
    private class TopHeightRunnable implements Runnable {
        @Override
        public void run() {
            topHeight = header.getMeasuredHeight();

            int[] position = new int[2];
            tab.getLocationInWindow(position);
            tabY = position[1];
        }
    }
}
